package top.laoshuzi.server.camera.config

import io.swagger.annotations.Api
import io.swagger.annotations.ApiOperation
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.web.servlet.config.annotation.EnableWebMvc
import springfox.documentation.builders.ApiInfoBuilder
import springfox.documentation.builders.PathSelectors
import springfox.documentation.builders.RequestHandlerSelectors
import springfox.documentation.service.*
import springfox.documentation.spi.DocumentationType
import springfox.documentation.spi.service.contexts.SecurityContext
import springfox.documentation.spring.web.plugins.Docket
import springfox.documentation.swagger2.annotations.EnableSwagger2

import java.util.Collections

@Configuration
@EnableSwagger2
class SwaggerConfig {

    @Bean
    fun createRestApi(): Docket {
        return Docket(DocumentationType.SWAGGER_2)
                .securitySchemes(listOf(securityScheme()))
                .securityContexts(listOf(securityContext()))
                .apiInfo(apiInfo())
                //                .groupName("push-service")
                .select()
                .apis(RequestHandlerSelectors.withClassAnnotation(Api::class.java))
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation::class.java))
                .paths(PathSelectors.any())
                .build()
    }

    @Bean
    internal fun securityScheme(): SecurityScheme {
        return ApiKey("bearer", "Authorization", "header")
    }

    @Bean
    internal fun securityContext(): SecurityContext {
        val securityReference = SecurityReference.builder()
                .reference("bearer")
                .scopes(arrayOf())
                .build()

        return SecurityContext.builder()
                .securityReferences(listOf(securityReference))
                .build()
    }

    private fun apiInfo(): ApiInfo {
        return ApiInfoBuilder()
                .title("API文档")
                .description("IOT")
                .termsOfServiceUrl("")
                //                .contact(new Contact("xxx", "http://xxx", "xxx@xx.com"))
                .version("1.0")
                .build()
    }

}
